package com.doubledr.light_lock.Task;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.widget.Toast;

import com.doubledr.light_lock.R;
import com.doubledr.light_lock.utils.FileManager;
import com.doubledr.light_lock.utils.FileProgress;

public class DecryptTask extends AsyncTask<String[],Integer,String> {
    Context mContext;
    ProgressDialog pgDialog;
    int mPwd = 123;
    boolean bSuccess = true;

    public  DecryptTask(Context context,int pwd){
        this.mContext = context;
        this.mPwd = pwd;
        pgDialog = new ProgressDialog(mContext);
        pgDialog.setTitle(context.getString(R.string.tip));
        pgDialog.setIcon(R.drawable.ic_launcher_round);
        pgDialog.setMessage(context.getString(R.string.de_progress_wait));
        pgDialog.setCancelable(false);
        pgDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    }
    @Override
    protected String doInBackground(String[]... strings) {
        try {
            FileManager.deCryptFile(strings[0][0], strings[0][1],mPwd, new FileProgress() {
                @Override
                public void getProgress(Integer d) {
                    publishProgress(d);
                }
            });
        }
        catch (Exception e){

        }
        return null;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //开始进行加密,显示对话框
        pgDialog.show();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        pgDialog.setProgress(values[0]);
        if(values[0] == -1){
            bSuccess = false;
            Toast toast=Toast.makeText(mContext, R.string.decrypt_failed,Toast.LENGTH_SHORT    );
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        pgDialog.cancel();
        if(bSuccess){
            new AlertDialog.Builder(mContext)
                    .setTitle(R.string.tip)
                    .setMessage(R.string.finish_view)
                    .setPositiveButton(R.string.sure, null)
                    .show();
        }
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        pgDialog.cancel();
    }
}
